package com.controller;

import com.Service.ExaminationprojectService;
import com.Service.SheetService;
import com.model.pojo.Examinationproject;
import com.model.pojo.Listing;

import com.model.pojo.Sheet;
import com.model.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("sheet")
public class SheetController {

    @Resource
    private Sheet sheet;
    @Resource
    private SheetService sheetService;
    @Resource
    private ExaminationprojectService examinationprojectService;

    @RequestMapping("printo")
    public String Printo(HttpSession session, Model model, HttpServletRequest request, String pagesNumber) {
        List<Listing> listtingList = (List) session.getAttribute("listtingList");
        List<Listing> Listting = (List) session.getAttribute("Listting");
        List<Sheet> sheetList = new ArrayList<Sheet>();
        int pagesNumber1 = 0;
        if (pagesNumber != null || ("").equals(pagesNumber)) {
            pagesNumber1 = Integer.valueOf(pagesNumber);

        }


        for (int i = 0; i < listtingList.size(); i++) {

            for (int j = 0; j < Listting.size(); j++) {

                if (listtingList.get(i).getListingSerialNumber() == Listting.get(j).getListingSerialNumber() || listtingList.get(i).getListingSerialNumber().equals(Listting.get(j).getListingSerialNumber())) {


                    sheet = new Sheet();
                    sheet.setListingSerialNumber(listtingList.get(i).getListingSerialNumber());
                    sheet.setSheetDepartment(listtingList.get(i).getPortfolio().getPortfolioDepartment());
                    sheet.setSheetDimensioncode(Listting.get(j).getListingSerialNumber() + Listting.get(j).getMedicalCardNumber());
                    sheet.setSheetProject(listtingList.get(i).getListingProject());
                    sheet.setSheetName(Listting.get(j).getBuildcard().getBuildCardName());
                    sheet.setSheetSex(Listting.get(j).getBuildcard().getBuildCardSex());
                    sheetList.add(sheet);

                }
            }

        }
        session.setAttribute("sheetList", sheetList);

        return "printCopy";
    }

    @RequestMapping("printInput")
    public String printInput(HttpSession session, Model model) {
        List<Sheet> sheetList = (List<Sheet>) session.getAttribute("sheetList");
        boolean bl = sheetService.printInput(sheetList);
        if (bl == true) {
            session.setAttribute("message", "存入");
        }

        return "redirect:/Listing/Listing";
    }


    @RequestMapping("summary")
    public String summary(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        List<Sheet> summaryList = sheetService.summary(user.getRoomName(), "");
        session.setAttribute("summaryList", summaryList);

        return "printo";
    }

    @ResponseBody
    @RequestMapping("/contente")
    public List<Sheet> contente(HttpSession session, String val) {
        System.out.println(val);
        User user = (User) session.getAttribute("user");
        List<Sheet> summaryLis = sheetService.summary(user.getRoomName(), val);
        session.setAttribute("summaryLis", summaryLis);
        return summaryLis;
    }


    @RequestMapping("details")
    public String details(HttpSession session, String num, String serialNumber, String Dimensioncode) {

        List<Examinationproject> ExaminationprojectList = sheetService.details(Dimensioncode);
        List<Examinationproject> projectList = new ArrayList<Examinationproject>();
        Map map = new HashMap();
        int number = 1;


        for (int i = 0; i < ExaminationprojectList.size(); i++) {

            if (i == 0) {
                map.put(number, ExaminationprojectList.get(i).getMedicalSheetId());
                number++;

            } else if (i > 0) {

                if (!ExaminationprojectList.get(i - 1).getMedicalSheetId().trim().equals(ExaminationprojectList.get(i).getMedicalSheetId().trim())) {

                    map.put(number, ExaminationprojectList.get(i).getMedicalSheetId());
                    number++;
                }


            }

            if (serialNumber != null && !serialNumber.equals("")) {
                num = (String) map.get(Integer.valueOf(serialNumber));
                System.out.println(num + "测试");
            }
            if (ExaminationprojectList.get(i).getMedicalSheetId().equals(num)) {
                projectList.add(ExaminationprojectList.get(i));
            }
        }
        for (Object key : map.keySet()) {

            if (map.get(key).equals(num)) {
                session.setAttribute("serialNumber", key);
            }
        }


        session.setAttribute("mapSize", map.size());
        session.setAttribute("projectList", projectList);


        return "printoxiaojie";
    }

    @ResponseBody
    @RequestMapping("printSummary")
    public boolean printSummary(HttpSession session, String summaryid, String summary) {
        List<Examinationproject> projectList1 = (List<Examinationproject>) session.getAttribute("projectList");
        boolean bl = false;
        System.out.println(projectList1.get(0).getExaminationprojectId());
        System.out.println(summaryid);
        if (projectList1.get(0).getExaminationprojectId().equals(summaryid)) {

            bl = sheetService.printSummary(projectList1, summary);

            session.setAttribute("cheng", "成功");
        }

        return bl;
    }

    /**
     * 数据源
     */


    @RequestMapping("/doSheet")
    public String doSheet(HttpSession session) {//欢华

        User user = (User) session.getAttribute("user");
        session.setAttribute("msg", "接收成功");
        if (user != null) {
            //如果这个登录者是管理员 给原始集合
            if ("管理员".equals(user.getUserPermissions())) {
                List<Sheet> sheetList = sheetService.findSheet();
                session.setAttribute("sheetList", sheetList);

            } else {
                List<Sheet> sheetList = sheetService.findSheet();
                List<Sheet> sheetListOne = new ArrayList<Sheet>();
                if (sheetList != null && sheetList.size() > 0) {

                    System.out.println(user);

                    // 从原始集合筛选出和登录者科室一样的
                    for (Sheet one : sheetList) {

                        if (one.getSheetDepartment().equals(user.getRoomName())) {
                            sheetListOne.add(one);
                        }
                    }
                }

                session.setAttribute("sheetList", sheetListOne);
                System.out.println(sheetListOne);
            }
        }


        return "sheetlist";
    }

    @RequestMapping("/doSheetJieShou")
    public String doSheetJieShou(@RequestParam("id") Integer id, HttpSession session) {

        boolean flag = sheetService.upadteById(id);
        List<Sheet> list=(List<Sheet>) session.getAttribute("sheetList");
        List<Sheet> listone=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            if (id==Integer.valueOf(list.get(i).getSheetId())){
                listone.add(list.get(i));

            }
        }
        List<Examinationproject> examinationproject = examinationprojectService.searcho(listone.get(0).getSheetProject().trim());
        System.out.println(examinationproject);
        session.setAttribute("listone",listone);
        session.setAttribute("examinationproject",examinationproject);
        return "changeExaminationproject";
    }


}
